package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.dto.LogDto;
import com.crm.entity.Log;
import com.crm.mapper.LogMapper;
import com.crm.result.PageResult;
import com.crm.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;

@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService  {
    @Autowired
    private LogMapper logMapper;

    /**
     * 分页查询日志
     * @param
     * @return
     **/
    @Override
    public PageResult pageLog(LogDto logDto) {
        LambdaQueryWrapper<Log> wrapper = new LambdaQueryWrapper<>();
        HashMap<String, String> params = new HashMap<>();
        params.put("beginTime", logDto.getBeginTime());
        params.put("endTime", logDto.getEndTime());
        String beginTime = params.get("beginTime");
        String endTime = params.get("endTime");
        if (beginTime != null && endTime != null) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate beginDate = LocalDate.parse(beginTime, formatter);
            LocalDate endDate = LocalDate.parse(endTime, formatter);
            wrapper.ge(Log::getOperTime, beginDate);
            wrapper.le(Log::getOperTime, endDate);
        }
        Page<Log> page = new Page<>(logDto.getPageNum(), logDto.getPageSize());
        Page<Log> logPage = logMapper.selectPage(page, wrapper);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(logPage.getTotal());
        pageResult.setRows(logPage.getRecords());
        return pageResult;
    }
    /**
     * 删除日志
     * @param
     * @return
     **/
    @Override
    public void removeById(Integer operId) {
        logMapper.deleteByoperId(operId);
    }
    /**
     * 清空日志
     * @param
     * @return
     **/
    @Override
    public void cleanLog() {
        logMapper.cleanAll();
    }
}
